package com.group.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.group.admin.dao.ShopCommodityDao;
import com.group.admin.entity.ShopCommodityEntity;
import com.group.admin.entity.ShopStockTaskDetailEntity;
import com.group.admin.entity.ShopStockTaskEntity;
import com.group.admin.service.ShopCommodityService;
import com.group.admin.service.ShopStockTaskDetailService;
import com.group.admin.service.ShopStockTaskService;
import com.group.admin.vo.ShopSku;
import com.group.common.to.SeckillOrderTo;
import com.group.common.utils.PageUtils;
import com.group.common.utils.Query;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;


@Service("shopCommodityService")
public class ShopCommodityServiceImpl extends ServiceImpl<ShopCommodityDao, ShopCommodityEntity> implements ShopCommodityService {
    @Autowired
    ShopStockTaskService shopStockTaskService;
    @Autowired
    ShopStockTaskDetailService shopStockTaskDetailService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //定义个null值
        String key = (String) params.get("key");
        QueryWrapper<ShopCommodityEntity> queryWrapper = new QueryWrapper<>();
        //模糊查询
        if (StringUtils.isNotBlank(key)){
            queryWrapper.like("commodity_name",key);
        }
        IPage<ShopCommodityEntity> page = this.page(
                new Query<ShopCommodityEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<ShopSku> skulist() {
        //sql联查商品全部信息
        return baseMapper.skulist();
    }
    @Transactional
    @Override
    public void lockStock(SeckillOrderTo to) {
        //添加任务表
        ShopStockTaskEntity taskEntity = new ShopStockTaskEntity();
        taskEntity.setOrderSn(to.getOrderSn());
        shopStockTaskService.save(taskEntity);
        //添加任务详情表
        ShopStockTaskDetailEntity detailEntity = new ShopStockTaskDetailEntity();
        detailEntity.setSkuId(to.getSkuId());
        detailEntity.setSkuNumber(to.getOrderNumber());
        detailEntity.setTaskId(taskEntity.getId());
        detailEntity.setLockStatus(1);
        shopStockTaskDetailService.save(detailEntity);
        //锁定库存
        ShopCommodityEntity commodityEntity = baseMapper.selectById(to.getSkuId());
        commodityEntity.setStockLock(commodityEntity.getStockLock()+to.getOrderNumber());
        baseMapper.updateById(commodityEntity);
    }
    @Transactional
    @Override
    public void releaseStock(SeckillOrderTo to) {
        //查询当前订单的任务单id
        ShopStockTaskEntity taskEntity = shopStockTaskService.getOne(new QueryWrapper<ShopStockTaskEntity>().eq("order_sn", to.getOrderSn()));
        //根据skuId和taskId查找对应任务详情  修改锁定状态为已解锁
        QueryWrapper<ShopStockTaskDetailEntity> wrapper = new QueryWrapper<ShopStockTaskDetailEntity>().eq("sku_id", to.getSkuId()).eq("task_id", taskEntity.getId());
        //查找当前sku对应的任务详情状态  不为已解锁才进行解锁
        ShopStockTaskDetailEntity detailEntity = shopStockTaskDetailService.getOne(wrapper);
        if (detailEntity.getLockStatus()!=2){
            detailEntity.setLockStatus(2);
            shopStockTaskDetailService.updateById(detailEntity);
            ShopCommodityEntity commodityEntity = baseMapper.selectById(to.getSkuId());
            commodityEntity.setStockLock(commodityEntity.getStockLock()-to.getOrderNumber());
            baseMapper.updateById(commodityEntity);
        }
    }


}